package com.neu.service;


import com.neu.entity.Invitation;
import com.neu.entity.Project;
import com.neu.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.net.URLEncoder;
import java.util.List;

@Service
public class EmailService {

    @Autowired
    private JavaMailSender mailSender;
    //TODO ngrok http 5173
    //还得改跨域
    private String invitationUrl = "https://9713-2001-da8-9000-a835-8481-fb92-df55-1a39.ngrok-free.app";

    public void sendSimpleEmail(String to, String subject, String text) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("3487393070@qq.com"); // 设置发件人地址
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
        mailSender.send(message);
    }


    public void sendHtmlEmail(String to, String subject, String htmlContent) throws Exception {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("3487393070@qq.com");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(htmlContent, true); // true 表示发送 HTML 邮件
        helper.addAttachment("myfile.jpg", new File("D:\\IDEALworkspace\\UtilTrain\\Mail\\src\\main\\resources\\122.jpg"));

        mailSender.send(message);
    }

    public void sendInviteEmails(List<Invitation> invitations, Project project, Role role) throws Exception {
        for (Invitation invitation : invitations) {
            String token = invitation.getToken().trim(); // 确保没有额外的空格
            // 如果 token 包含特殊字符，可能会导致 URL 解析错误。这里使用 URLEncoder 编码
            String link = invitationUrl + "/invite/accept?token=" + URLEncoder.encode(token, "UTF-8");
            String subject = "您被邀请加入项目 " + project.getProjectName();
            String content = String.format(
                    "<p>您好，</p>" +
                            "<p>您被邀请加入项目 <strong>%s</strong>，角色为 <strong>%s</strong>。</p>" +
                            "<p>请点击以下链接接受或拒绝邀请：</p>" +
                            "<p><a href=\"%s\">接受邀请</a></p>" +
                            "<p>\n此链接将在7天后过期。</p>",
                    project.getProjectName(),
                    role.getRoleName(),
                    link
            );
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
            message.setFrom("3487393070@qq.com"); // 设置发件人地址
            helper.setTo(invitation.getInviteeEmail());
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);

        }
    }
}